package com.pet.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pet.service.SymptomService;
import com.pet.service.SysOrderService;
import com.pet.system.Symptom;
import com.pet.system.SysOrder;
import com.pet.utils.Result;
import com.pet.utils.SnowFlakeGenerateIdWorker;
import com.pet.vo.SysOrderQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.text.DateFormat;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import static java.lang.String.format;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author whd9-`
 * @since 2023-08-03
 */
@Api("订单接口")
@RestController
@RequestMapping("/lovePet/system/sysOrder")
@CrossOrigin("*")
public class SysOrderController {
    @Autowired
    SymptomService symptomService;
    @Autowired
    SysOrderService sysOrderService;
    @ApiOperation("接收订单")
    @PostMapping("pushOrder")
    public Result pushOrder(@RequestBody SysOrder sysOrder){
        //生成唯一订单号
        String s = new SnowFlakeGenerateIdWorker(0, 0).generateNextId();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String id = simpleDateFormat.format(new Date().getTime()) + s;
        sysOrder.getSymptom().setOrderId(id);
        sysOrder.setId(Long.parseLong(id));
        //存入订单表
        boolean b = sysOrderService.save(sysOrder);
        //存入症状表
        symptomService.save(sysOrder.getSymptom());
        return b?Result.ok():Result.fail();
    }


    @GetMapping("/findAllOrder")
    public Result findAllOrder(){
        List<SysOrder> list = sysOrderService.list();
        return Result.ok(list);
    }

    @GetMapping("/findDataByUserId/{userId}")
    public Result findDataByUserId(@PathVariable String userId) {
//        SysDoctor sysDoctor = sysDoctorService.getById(doctorId);
        List<SysOrder> orderList = sysOrderService
                .list(new LambdaQueryWrapper<SysOrder>()
                        .eq(SysOrder::getUserId,userId )
                        .eq(SysOrder::getStatus,1));
        return Result.ok(orderList);
    }


    //分页和条件查询
    @ApiOperation("分页和条件查询")
    @PostMapping("{page}/{limit}")
    public Result selectDataPageByVo(@PathVariable Long page,
                                     @PathVariable Long limit,
                                     @ApiParam("包装的查询分类名称名称") @RequestBody SysOrderQueryVo sysOrderQueryVo) {
//        System.out.println("=========" + sysNoticeQueryVo.getMessage());
        IPage<SysOrder> iPage = new Page<>(page, limit);
        iPage = this.sysOrderService.selectpage(iPage, sysOrderQueryVo);
        return Result.ok(iPage);
    }


    // 添加数据
    @PostMapping("/addData")
    public Result addData(@RequestBody SysOrder sysOrder) {
        sysOrder.setConsultationId(generateRandomString());
        boolean b = this.sysOrderService.save(sysOrder);
        return b ? Result.ok(sysOrder.getLicenseNumber()) : Result.fail();
    }

    //根据id获取数据
    @GetMapping("/findDataById/{id}")
    public Result findDataById(@PathVariable Long id) {
        SysOrder sysOrder = this.sysOrderService.getById(id);
        return Result.ok(sysOrder);
    }


    // 实现修改
    @PostMapping("/updateData")
    public Result updateData(@RequestBody SysOrder sysOrder) {
        boolean b = this.sysOrderService.updateById(sysOrder);
        return b ? Result.ok() : Result.fail();

    }

    @ApiOperation("根据id去移除一个宠数据")
    @DeleteMapping("/removeDataById/{id}")
    public Result removeDataById(@PathVariable Long id) {
        boolean b = this.sysOrderService.removeById(id);
        return b ? Result.ok() : Result.fail();
    }


    public String generateRandomString() {
        // 生成当前时间戳
        String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());

        // 随机数的位数
        int numDigits = 36 - timestamp.length();

        // 生成随机数
        Random random = new Random();
        int randomNum = random.nextInt((int) Math.pow(10, numDigits));

        // 拼接字符串
        String formattedRandomNum = String.format("%0" + numDigits + "d", randomNum);
        String result = timestamp + formattedRandomNum;

        return result;
    }


}

